const express = require('express');

// 实体类
const ObjId = require('../entity/common/ObjId');
const Response = require('../entity/http/Response');

// 业务层
const faultService = require('../service/fault');

const router = express.Router();

/**
 * @apiName 获取所有故障信息接口
 */
router.get('/faultList', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /fault/faultList`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    faultService.getAllfault(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有故障记录响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有故障记录时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
    // mock
    // res.status(200).json(new Response().success([
    //     {
    //         id: 1,
    //         faultTime: '2024-02-15 16:17:49',     // 故障时间
    //         faultCode: 'E006',                    // 故障码
    //         modelSeries: 'WS-9200',               // 设备型号系列
    //         equipmentId: 'EQKT9',                 // 设备ID
    //         hospital: '广东省中医院',              // 医院
    //         department: '骨科',                   // 科室
    //         enzymeType: '多酶清洗剂A型',           // 酶液
    //         disinfectantType: '过氧乙酸',          // 消毒液
    //         faultDescription: '排水系统堵塞',      // 故障描述
    //         createdAt: '2024-02-15 16:17:49',     // 创建时间
    //         updatedAt: '2024-02-15 16:17:49'      // 更新时间
    //     },
    //     {
    //         id: 2,
    //         faultTime: '2024-02-15 16:17:49',     // 故障时间
    //         faultCode: 'E006',                    // 故障码
    //         modelSeries: 'WS-9200',               // 设备型号系列
    //         equipmentId: 'EQKT9',                 // 设备ID
    //         hospital: '广东省中医院',              // 医院
    //         department: '骨科',                   // 科室
    //         enzymeType: '多酶清洗剂A型',           // 酶液
    //         disinfectantType: '过氧乙酸',          // 消毒液
    //         faultDescription: '排水系统堵塞',      // 故障描述
    //         createdAt: '2024-02-15 16:17:49',     // 创建时间
    //         updatedAt: '2024-02-15 16:17:49'      // 更新时间
    //     },
    // ]))
});

/**
 * @apiName 删除故障信息接口
 */
router.delete('/fault', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 DELETE /fault/fault`);
    const { uid } = req.token_payload.data;
    let id;
    // 接收参数
    try {
        id = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "故障信息Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 故障信息Id ${ JSON.stringify(id) }`);
    // 进入业务层
    faultService.deletefault(req.requestId, uid, id).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 删除故障记录响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 删除故障记录时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 单条故障信息已读接口
 */
router.post('/readFault', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /fault/readFault`);
    const { uid } = req.token_payload.data;
    let id;
    // 接收参数
    try {
        id = new ObjId(req.body.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "故障信息Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 故障信息Id ${ JSON.stringify(id) }`);
    // 进入业务层
    faultService.readFault(req.requestId, uid, id).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 已读故障记录响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 已读故障记录时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 故障信息一键已读接口
 */


module.exports = router;